<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Zhilvyun_Admin_Config extends Stourweb_Controller
{
    /**
     * 配置内容
     * @throws Kohana_Exception
     */
    public function action_index()
    {
        if ($this->request->method() == 'POST')
        {
            $consume_callback_url = Arr::get($_POST, 'consume_callback_url');

            $partnerId = Arr::get($_POST, 'partnerId');

            $id = Arr::get($_POST, 'id');
            $data = array(
                'partnerId'=>$partnerId,
                'consume_callback_url' => $consume_callback_url,
                'modtime' => time()
            );
            if($id>0)
            {
                $query = DB::update('distribute_zhilvyun_config')
                    ->set($data)->where('id', '=', $id)->execute();
            }
            else
            {
                $data = array(
                    'app_id' => 'AKIDekwdCbS7Lb3yqPEaUw5uqVYkagrgoCtMnd32',
                    'app_secret' => 'f0t6chnuCxF7Yjfou5o29L5dqiD88P9pOivkcPzw',
                    'partnerId' => $partnerId,
                    'addtime' => time(),
                    'consume_callback_url' => $consume_callback_url,
                    'modtime'=>null
                );
                $query = DB::insert('distribute_zhilvyun_config',array_keys($data))
                    ->values(array_values($data))->execute();
            }
            if ($query)
            {
                echo json_encode(array('status' => true));
                exit();
            }
            echo json_encode(array('status' => false));
            exit();
        }
        $newdata = DB::select()->from('distribute_zhilvyun_config')->execute()->current();
        $this->assign('newdata', $newdata);
        $this->display('zhilvyun/admin/ctrip/config');
    }
}